package com.zc.campus_shops.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;

import com.zc.campus_shops.dto.ImageHolder;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ImageUtil {
	// 获取路径
	private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
	private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private static final Random r = new Random();

    public static String generateThumbnail(ImageHolder thumbnail, String targetAddr) {
    	System.out.println("basePath:" + basePath);
		// 文件名
		String realFileName = getRandomFileName();
		System.out.println("realFileName" + realFileName);
		// 后缀
		String extension = getFileExtension(thumbnail.getImageName());
		System.out.println("extension:" + extension);
		// 相对路径
		makeDirPath(targetAddr);
		String relativeAddr = targetAddr + realFileName + extension;
		System.out.println("路径" + relativeAddr);
		// 组成新生成文件的路径
		File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
		System.out.println("dest" + dest);
		System.out.println("basePath:" + basePath);
		try {
			Thumbnails.of(thumbnail.getImage()).size(200, 200)
					.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/hot.jpg")), 0.25f)
					.outputQuality(0.8f).toFile(dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return relativeAddr;
	}


	/**
	 * 创建目标路径所涉及的目录 即 home/zc/image/xxx.jpg home zc image 都要自动创建出来
	 * 
	 * @param targetAddr
	 */

	private static void makeDirPath(String targetAddr) {
		// TODO Auto-generated method stub
		// 获取目标的全路径
		String realFileParentPath = PathUtil.getImgBasePath() + targetAddr;
		File dirPath = new File(realFileParentPath);
		if (!dirPath.exists()) {
			dirPath.mkdirs();
		}

	}

	/**
	 * 获取输入文件流的扩展名 如 .jpg .png
	 * 
	 * @param thumbnail
	 * @return
	 */

	private static String getFileExtension(String fileName) {
		// TODO Auto-generated method stub

		return fileName.substring(fileName.lastIndexOf("."));
	}

	/**
	 * 生成随机文件名 当前年月日小时分钟秒+五位随机数
	 * 
	 * @return
	 */

	private static String getRandomFileName() {
		// TODO Auto-generated method stub
		// 获取随机5位数
		int rannum = r.nextInt(89999) + 10000;
		String nowTimeStr = sDateFormat.format(new Date());

		return nowTimeStr + rannum;
	}

	public static void main(String[] args) throws IOException {

		Thumbnails.of(new File("E:/javaee/image/xhr.jpg")).size(200, 200)
				.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/hot.jpg")), 0.5f)
				.outputQuality(0.8f).toFile("E:/javaee/image/xhrnew.jpg");

	}

	/**
	 * 删除旧图片的方法
	 * store是文件路径 还是目录的路径，
	 * 如果是文件路径则删除文件
	 * 如果是目录文件 则删除该目录下的所有文件
	 * @param storePath
	 */
	public static void deleteFileOrPath(String storePath){
		//获取文件的全路径
		
		File fileOrPath = new File(PathUtil.getImgBasePath() + storePath);
		
		if(fileOrPath.exists()){
			//如果是目录
			if(fileOrPath.isDirectory()){
				File files[] = fileOrPath.listFiles();
				for(int i =0; i < files.length; i++){
					files[i].delete();
				}
			}
			//如果是文件,直接删除
			fileOrPath.delete();
		}
		
		
	}


	public static String generateNormalImg(ImageHolder thumbnail, String targetAddr) {
		// 获取不重复的随机文件名
		String realFileName = getRandomFileName();
		// 后缀
		String extension = getFileExtension(thumbnail.getImageName());
		// 如果相对路径不存在 则自动创建
		makeDirPath(targetAddr);
		//获取文件存储的相对路径
		String relativeAddr = targetAddr + realFileName + extension;
		System.out.println("路径" + relativeAddr);
		// 组成新生成文件的路径
		File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
		System.out.println("dest" + dest);
		System.out.println("basePath:" + basePath);
		try {
			Thumbnails.of(thumbnail.getImage()).size(337, 640)
					.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/hot.jpg")), 0.25f)
					.outputQuality(0.9f).toFile(dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return relativeAddr;
	}
}
